package com.jm.offer.easy;

import com.jm.offer.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Description https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/
 * @date 2022/4/27 8:32
 */
public class _55_I二叉树的深度 {

    public static void main(String[] args) {
        System.out.println(maxDepth(new TreeNode(1)));
    }

    public static int maxDepth(TreeNode root) {
        // 层序遍历即可
        if (root == null){
            return 0;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int height = 0;
        // 记录每一层元素的数量
        int levelSize = 1;
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            levelSize--;

            if (node.right != null){
                queue.offer(node.right);
            }
            if (node.left != null){
                queue.offer(node.left);
            }
            // 等于0表示当前层访问完毕。
            if (levelSize == 0){
                levelSize = queue.size();
                height++;
            }
        }
        return height;
    }
}
